package org.agony2d.gpu.core {
	import org.agony2d.core.agony_internal;
	import org.agony2d.debug.Logger;
	import org.agony2d.gpu.FusionAA;
	import org.agony2d.gpu.supportClasses.buffers.BuffersManager;
	
	use namespace agony_internal;
	
	/** dirty: vertexChanged × altered × texChanged × frame(rect) */
public class ImageAA extends PixelsAA {
	
	final public function get textureData() : String {
		return m_textureData
	}
	
	public function set textureData( v:String ) : void {
		if (m_textureData != v) {
			m_textureData = v
			if (m_fullTex) {
				m_texChanged = true
			}
		}
	}
	
	override public function get width() : int {
		if (m_texChanged) {
			m_renderTex = m_fullTex.getRegion(m_textureData)
		}
		return super.width
	}
	
	override public function get height() : int {
		if (m_texChanged) {
			m_renderTex = m_fullTex.getRegion(m_textureData)
		}
		return super.height
	}
	
	
	
	agony_internal var m_textureData:String
	agony_internal var m_texChanged:Boolean
	
	
	
	[Inline]
	final override agony_internal function doUpdateUpward() : void {
		var node:NodeAA
		
		if (m_altered) {
			if (m_texChanged) {
				m_renderTex = m_fullTex.getRegion(m_textureData)
			}
			this.updateByAlone()
		}
		else if (m_texChanged) {
			m_renderTex = m_fullTex.getRegion(m_textureData)
			if (m_renderTex.m_frame) {
				this.updateByAlone()
			}
		}
		cachedMatrix.copyFrom(m_matrix_A)
		node = m_parent
		while (node && !(node is WorldAA)) {
			if (node.m_altered) {
				node.updateByAlone()
			}
			cachedMatrix.concat(node.m_matrix_A)
			node = node.m_parent
		}
	}
	
	[Inline]
	final override agony_internal function attach( parent:NodeAA ) : void {
		if (!parent.m_fullTex) {
			Logger.reportError(this, "attach", "Parent hasn't been added to any GroupAA...!!")
		}
		if (m_parent) {
			if (m_fullTex != parent.m_fullTex) {
				Logger.reportError(this, "attach", "Full Texture alters...!!")
			}
			m_parent.removeNode(this)
			m_altered = true
		}
		else {
			m_fullTex = parent.m_fullTex
			if (m_textureData != null) {
				m_texChanged = true
			}
		}
		m_parent = parent
		m_world = parent.m_world
	}
	
	[Inline]
	final override agony_internal function doPushToConvergence( vertexChanged:Boolean ) : void {
		if (vertexChanged) {
			if (m_texChanged) {
				m_renderTex = m_fullTex.getRegion(m_textureData)
				m_combine.setUV(m_renderTex)
				m_texChanged = false
			}
			if (m_altered || m_renderTex.m_frame) {
				this.doUpdateDownward()
				m_altered = false
			}
			else {
				m_matrix_B.copyFrom(m_matrix_A)
			}
			m_matrix_B.concat(m_parent.m_matrix_B)
			m_combine.setVertexCoords(m_renderTex.m_realityWidth, m_renderTex.m_realityHeight, m_matrix_B)
		}
		else if (m_altered) {
			if (m_texChanged) {
				m_renderTex = m_fullTex.getRegion(m_textureData)
				m_combine.setUV(m_renderTex)
				m_texChanged = false
			}
			this.doUpdateDownward()
			m_altered = false
			m_matrix_B.concat(m_parent.m_matrix_B)
			m_combine.setVertexCoords(m_renderTex.m_realityWidth, m_renderTex.m_realityHeight, m_matrix_B)
		}
		else if (m_texChanged) {
			m_renderTex = m_fullTex.getRegion(m_textureData)
			m_combine.setUV(m_renderTex)
			m_texChanged = false
			if (m_renderTex.m_frame) {
				this.doUpdateDownward()
				m_matrix_B.concat(m_parent.m_matrix_B)
				m_combine.setVertexCoords(m_renderTex.m_realityWidth, m_renderTex.m_realityHeight, m_matrix_B)
			}
			else {
				m_combine.setVertexCoordsFixed(m_renderTex.m_realityWidth, m_renderTex.m_realityHeight, m_matrix_B)
			}
		}
		BuffersManager.g_bytes.writeBytes(m_combine.m_bytes)
	}
	
}
}